package com.audaque.springboot.foshanupload.core.util.encode;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class JavaBase64Utils {


  public static String encodeHex(byte[] input) {
	return Hex.encodeHexString(input);
  }

  public static byte[] decodeHex(String input) {
	try {
	  return Hex.decodeHex(input);
	} catch (DecoderException var2) {
	  throw new RuntimeException(var2.getLocalizedMessage());
	}
  }

  public static String encodeBase64(byte[] input) {
	return Base64.getEncoder().encodeToString(input);
  }

  public static String encodeBase64UTF8(String input) {
	try {
	  return encodeBase64(input.getBytes("UTF-8"));
	} catch (UnsupportedEncodingException var2) {
	  return encodeBase64(input.getBytes());
	}
  }

  public static String encodeUrlSafeBase64(byte[] input) {
	return Base64.getUrlEncoder().encodeToString(input);
  }

  public static byte[] decodeBase64(String input) {
	return Base64.getDecoder().decode(input);
  }

  public static String decodeBase64UTF8(String input) {
	try {
	  return new String(decodeBase64(input), "UTF-8");
	} catch (UnsupportedEncodingException var2) {
	  return new String(decodeBase64(input));
	}
  }
}
